/**
 * Created by kitabi on 2017/6/16.
 */
var userInfo={

};

/**
 * 获取用户数据
 */
userInfo.queryList= function(){


                $("#mainTable").bootstrapTable({

                        method:'POST',
                        dataType:'json',
                        contentType: "application/x-www-form-urlencoded",
                        cache: false,
                        striped: true,                              //是否显示行间隔色
                        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                        url:"/user/qryUserList",
                        // height: $(window).height() - 110,
                        // width:$(window).width(),
                        showColumns:true,
                        pagination:true,
                        // queryParams : queryParams,
                        minimumCountColumns:2,
                        pageNumber:1,                       //初始化加载第一页，默认第一页
                        pageSize: 10,                       //每页的记录行数（*）
                        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
                        showExport: true,
                        exportDataType: 'all',


                        paginationPreText:"<",
                        paginationNextText:">",
                        queryParams: function(params) {
                            var userStatus= $("#userStatus").val();
                            var userDateStart=$("#userDateStart").val();
                            var userDateEnd=$("#userDateEnd").val();
                            var userSearch= $("#userSearch").val();
                            return {
                                cPage: (params.offset/params.limit)+1,
                                pSize: params.limit,
                                status:userStatus,
                                phone:userSearch,
                                start:userDateStart,
                                end:userDateEnd
                            };
                        },
                        columns: [
                            {
                                field: 'state',
                                checkbox: true,
                                align: 'center',
                                valign: 'middle'
                            },{
                            field: 'id',
                            title: '会员ID'
                        }, {
                            field: 'nick_name',
                            title: '昵称'
                        }, {
                            field: 'head_img',
                            title: '头像',
                                formatter: function(value,row,index){
                                    //value：当前field的值，即id
                                    //row：当前行的数据
                                    var img_url= row.head_img;
                                    if(img_url==null || img_url ==""){
                                        return  '<img src="/newshop/assets/images/grzx_80.png" style="width: 40px;height: 40px;">';
                                    }

                                    return  '<img src="'+img_url+'" style="width: 40px;height: 40px;">';
                                }
                        }, {
                            field: 'phone',
                            title: '手机号'
                        }, {
                            field: 'order_price_num',
                            title: '营业额'
                        }, {
                            field: 'return_price_num',
                            title: '推广费'
                        }, {
                            field: 'parend_user_account',
                            title: '上级会员'
                        }, {
                            field: 'create_time',
                            title: '注册时间'
                        }, {
                            field: 'status',
                            title: '状态',
                            formatter: function(value,row,index){
                                if(value == 1){
                                    return '正常';
                                }else{
                                    return '禁用';
                                }

                            }
                    }, {

                        field: 'status',
                        title: '操作',
                        align: 'center',

                            formatter: function(value,row,index){
                                //value：当前field的值，即id
                                //row：当前行的数据
                                var id= row.id;
                                var status='<button type="button" onclick="updateStatus('+id+',1)" style="margin-right: 5px;" class="btn btn-sm btn-success btn-trans">启用</button>';
                                if(value==1){
                                    status='<button type="button" onclick="updateStatus('+id+',0)" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">禁用</button>';
                                }
                                // var e='<button type="button" onclick="showDetail('+index+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">编辑</button>';
                                var d='<button type="button" onclick="showDetail('+id+')" style="margin-right: 5px;" class="btn btn-sm btn-info btn-trans">详情</button>';
                                return  status+d;
                            }
                        }]

                });

}

function updateStatus(id ,type){
    var param={
        id: id,
        u_status: type
    }
    Ajax.ajax({
        url: "/user/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function showDetail(index){


    $('#main-content').load('/user/getOrderDetail?id='+index);
}

$(function(){

    //开始\结束 日期选择
    $("#userDateStart").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true,

    });
    $("#userDateEnd").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true,

    });
    userInfo.queryList();

    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });
});